import {_decorator, Component, Label, Node, Sprite, UITransform} from 'cc';
import {AudioManager} from "db://assets/scripts/common/AudioManager";
import {UIManager} from "db://assets/scripts/common/UIManager";
import {EventManager} from "db://assets/scripts/common/EventManager";
import {PlayerDataManager} from "db://assets/scripts/common/PlayerDataManager";
import {AudioPaths} from "db://assets/scripts/common/AudioPaths";
import {EventType} from "db://assets/scripts/common/EventType";

const {ccclass, property} = _decorator;

/**
 * 复活界面
 */
@ccclass('ReviveController')
export class ReviveController extends Component {
    /**
     * 玩家图标
     */
    @property(Sprite)
    public playerIconSprite: Sprite;
    /**
     * 等级
     */
    @property(Label)
    public levelLabel: Label;
    /**
     * 遮罩
     */
    @property(Node)
    public maskNode: Node;
    /**
     * 倒计时
     */
    @property(Label)
    public countDownLabel: Label;

    /**
     * 倒计时
     */
    private countDown: number = 10;
    /**
     * 遮罩最大高度
     */
    private maskMaxHeight: number = 190;
    /**
     * 遮罩当前高度
     */
    private maskCurrentHeight: number = 0;
    /**
     * 回调
     */
    private callback: Function;

    update(deltaTime: number) {
        if (this.countDown > 0) {
            this.countDown -= deltaTime;
        }
    }

    /**
     * 设置倒计时
     *
     * @param value 倒计时
     */
    public setCountDown(value: number) {
        this.countDown = value;
        this.countDownLabel.string = `${Math.floor(this.countDown)}`;
        this.levelLabel.string = `${PlayerDataManager.getInstance().getPlayer().floor}`;

        this.maskCurrentHeight += this.maskMaxHeight / (this.countDown * 120);
        this.maskCurrentHeight = this.maskCurrentHeight >= this.maskMaxHeight ? this.maskMaxHeight : this.maskCurrentHeight;
        this.maskNode.getComponent(UITransform)?.setContentSize(260, this.maskCurrentHeight);

        if (value < 0) {
            this.close();
        }
    }

    /**
     * 获取倒计时
     */
    public getCountDown(): number {
        return this.countDown;
    }

    /**
     * 展示复活界面
     *
     * @param callback 回调
     */
    public show(callback?: Function) {
        this.countDown = 10;
        this.maskCurrentHeight = 0;
        this.callback = callback;
    }

    /**
     * 不需要复活
     */
    public onCancel() {
        // 播放声音
        AudioManager.getInstance().playSound(AudioPaths.clickSound);
        // 关闭界面
        this.close();
    }

    /**
     * 复活
     */
    public onRevive() {
        // 播放声音
        AudioManager.getInstance().playSound(AudioPaths.clickSound);
        // 隐藏复活界面
        UIManager.getInstance().hideDialog("revive/revivePanel");
        // 分发事件
        EventManager.dispatchEvent(EventType.ON_REVIVE);
    }

    /**
     * 关闭界面
     */
    private close() {
        // 隐藏战斗界面
        UIManager.getInstance().hideDialog("fight/fightPanel");
        // 隐藏复活界面
        UIManager.getInstance().hideDialog("revive/revivePanel");
        // 显示设置界面
        UIManager.getInstance().showDialog("settlement/settlementPanel");
        // 回调
        this.callback && this.callback();
    }

}